#ifndef PROJECTMODEL_H
#define PROJECTMODEL_H
#include "pugixml/pugixml.hpp"
#include <QDebug>
#include <string>
#include <vector>
struct stProject {
    std::string name; //工程名称
    std::string fileName; //文件名称默认与工程名称一致
    std::string desc;
    std::string path;
    std::string createTime;
    std::string modiTime;
};

struct stTcpClient {
    std::string ipaddrA;
    std::string ipaddrB;
    std::string portA;
    std::string portB;
    std::string localIpAddr; //本机IP
};
struct stTcpServer {
    std::string ipaddr;
    std::string port;
};
struct stUdp {
    std::string remoteIpaddr;
    std::string remotePort;
    std::string localIpaddr;
    std::string localPort;
};
struct stSerialPort {
    std::string name;
    std::string baudRate;
    std::string dataBit;
    std::string checkBit;
    std::string stopBit;
};

struct stCollectChannel {
    enum ChannelPortType {
        Port_Invented,
        Port_SerialPort
    };
    int id; //唯一标识符
    std::string name;
    std::string desc;
    std::string protoName; //协议名称
    std::string protocate; //厂家
    ChannelPortType portType; //端口类型
    std::string reserve; //备用参数
    stSerialPort serialport; //串口参数
};
//设备的私有参数
struct stPrivateParam {
    int id;
    std::string name;
    std::string value;
    std::string desc;
    stPrivateParam() {}
};

struct stCollecDevice {
    enum Intarface_Type {
        Intarface_TcpClinet,
        Intarface_TcpServer,
        Intarface_Udp,
        Intarface_Comm,
    };
    int id;
    std::string name;
    std::string desc;
    Intarface_Type InterfaceType;
    stTcpClient tcpClient;
    stTcpServer tcpServer;
    stUdp udp;
    int isDisable; //是否禁用，true为禁用,false为启用
    std::string devAddr; //设备地址
    std::string periodTime; //周期时间
    pugi::xml_node pointTable_node; //点表节点
    std::vector<stPrivateParam> params;
    pugi::xml_document* m_doc;

    stCollecDevice& operator=(const stCollecDevice& other)
    {
        this->id = other.id;
        this->name = other.name;
        this->desc = other.desc;
        this->InterfaceType = other.InterfaceType;
        this->tcpClient = other.tcpClient;
        this->tcpServer = other.tcpServer;
        this->udp = other.udp;
        this->isDisable = other.isDisable;
        this->devAddr = other.devAddr;
        this->periodTime = other.periodTime;
        this->params = other.params;

        createXML();
        if (!other.pointTable_node.empty()) {
            pugi::xml_node child_node = other.pointTable_node.first_child();
            while (child_node) {
                pointTable_node.append_copy(child_node);
                child_node = child_node.next_sibling();
            }
            qDebug() << "child name = " << other.pointTable_node.child("tag").attribute("name").value();
        }

        return *this;
    }
    void createXML()
    {
        m_doc = new pugi::xml_document();
        pointTable_node = m_doc->append_child("points");
        pointTable_node.append_attribute("a") = "abcd";
    }
    void deleteXML()
    {
        delete m_doc;
    }
};
struct stUpdateChannel {
    enum Intarface_Type {
        Intarface_TcpClinet,
        Intarface_TcpServer,
        Intarface_Udp,
        Intarface_Comm,
    };

    int id;
    std::string name;
    std::string desc;
    std::string protoName;
    std::string protoCate;
    Intarface_Type intarfaceType;
    stTcpClient tcpClient;
    stTcpServer tcpServer;
    stUdp udp;
    stSerialPort serialport; //串口参数
    int isDisable; //是否禁用，1为禁用,0为不禁用
    std::string devAddr; //设备地址
    std::string periodTime; //周期时间
    pugi::xml_node pointTable_node; //点表节点
    std::vector<stPrivateParam> params;
    pugi::xml_document* m_doc;

    stUpdateChannel& operator=(const stUpdateChannel& other)
    {
        this->id = other.id;
        this->name = other.name;
        this->desc = other.desc;
        this->protoCate = other.protoCate;
        this->protoName = other.protoName;
        this->intarfaceType = other.intarfaceType;
        this->tcpClient = other.tcpClient;
        this->tcpServer = other.tcpServer;
        this->serialport = other.serialport;
        this->udp = other.udp;
        this->isDisable = other.isDisable;
        this->devAddr = other.devAddr;
        this->periodTime = other.periodTime;
        this->params = other.params;

        createXML();
        if (!other.pointTable_node.empty()) {
            pugi::xml_node child_node = other.pointTable_node.first_child();
            while (child_node) {
                pointTable_node.append_copy(child_node);
                child_node = child_node.next_sibling();
            }
            qDebug() << "child name = " << other.pointTable_node.child("tag").attribute("name").value();
        }

        return *this;
    }
    void createXML()
    {
        m_doc = new pugi::xml_document();
        pointTable_node = m_doc->append_child("points");
        pointTable_node.append_attribute("a") = "abcd";
    }
    void deleteXML()
    {
        delete m_doc;
    }
};
#endif // PROJECTMODEL_H
